package com.itboye.textileexpres.shopcart;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.R.integer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.tbshopcar.listener.DecimalUtil;
import com.itboye.tbshopcar.listener.OnShoppingCartChangeListener;
import com.itboye.tbshopcar.listener.ShoppingCartBiz;
import com.itboye.textileexpes.beans.ShopCartModel;
import com.itboye.textileexpes.presenter.ShopCartPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.shopcart.ShopcartExpandableListViewAdapter.CheckInterface;
import com.itboye.textileexpres.shopcart.ShopcartExpandableListViewAdapter.ModifyCountInterface;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class ShopCart extends BaseFragment implements Observer, CheckInterface,
		ModifyCountInterface {
	AppAdapter mAdapter;
	ShopCartModel cartModel = new ShopCartModel();
	ShopCartPresenter presenter;
	private List<ShopCartModel> goodsList = new ArrayList<ShopCartModel>();
	private String uid;
	ExpandableListView exListView;
	TextView tvtextNumber;
	ImageView all_img;
	ImageView edit_all;
	RelativeLayout count_rel;
	RelativeLayout bottom;
	RelativeLayout no_data;
	boolean AllEdit = false;
	boolean GroupEdit = false;
	boolean AllSelect = false;
	boolean Select = false;
	TextView title;
	ImageView back, img_other;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_shopcar;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		presenter = new ShopCartPresenter(this);
		uid = (String) SPUtils.get(getActivity(), null, SpPublicParams.UID, "");
		cartModel.setUid("39");
		title.setText("购物车");
		back.setVisibility(View.GONE);
		presenter.getInfo(cartModel);

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ShopCartPresenter.QueryShopCartSuccessEvent) {
				goodsList.addAll((List<ShopCartModel>) entity.getData());
				exListView = (ExpandableListView) getActivity().findViewById(
						R.id.exListView);
				if (goodsList.size() == 0) {
					exListView.setVisibility(View.GONE);
					count_rel.setVisibility(View.GONE);
					bottom.setVisibility(View.GONE);
					no_data.setVisibility(View.VISIBLE);
				} else {
					exListView.setVisibility(View.VISIBLE);
					count_rel.setVisibility(View.VISIBLE);
					bottom.setVisibility(View.VISIBLE);
					no_data.setVisibility(View.GONE);
					mAdapter = new AppAdapter(getActivity(), goodsList);
					exListView.setAdapter(mAdapter);
					tvtextNumber.setText("" + goodsList.size());
					mAdapter.setCheckInterface(this);// 关键步骤1,设置复选框接口
					mAdapter.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口
					for (int i = 0; i < mAdapter.getGroupCount(); i++) {
						exListView.expandGroup(i);// 关键步骤3,初始化时，将ExpandableListView以展开的方式呈现
					}
					exListView
							.setOnGroupClickListener(new OnGroupClickListener() {
								@Override
								public boolean onGroupClick(
										ExpandableListView parent, View v,
										int groupPosition, long id) {
									// TODO Auto-generated method stub
									return true;
								}
							});
					OnClickListener clickListener = mAdapter.getListener();
					if (clickListener != null) {
						all_img.setOnClickListener(mAdapter.getListener());
						edit_all.setOnClickListener(mAdapter.getListener());

					}
				}

			} else if (entity.getEventType() == ShopCartPresenter.QueryShopCartFailureEvent) {
				toast("哈,");

			} else if (entity.getEventType() == ShopCartPresenter.DeleteShopCartSuccessEvent) {

			} else if (entity.getEventType() == ShopCartPresenter.DeleteShopCartFailureEvent) {

			} else if (entity.getEventType() == ShopCartPresenter.UpdateShopCartSuccessEvent) {

			} else if (entity.getEventType() == ShopCartPresenter.UpdateShopCartFailureEvent) {

			}
		}
	}

	class AppAdapter extends BaseExpandableListAdapter {
		CheckInterface checkInterface;
		ModifyCountInterface modifyCountInterface;
		Context context;
		List<ShopCartModel> goodsList;
		boolean isSelectAll = false;
		OnShoppingCartChangeListener mChangeListener;

		public AppAdapter(Context context, List<ShopCartModel> goodsList) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.goodsList = goodsList;
		}

		public void setOnShoppingCartChangeListener(
				OnShoppingCartChangeListener changeListener) {
			this.mChangeListener = changeListener;
		}

		public void setList(List<ShopCartModel> goodsList) {
			this.goodsList = goodsList;
			setSettleInfo();
		}

		private void setSettleInfo() {
			// TODO Auto-generated method stub
			String[] infos = ShoppingCartBiz.getShoppingCount(goodsList);
			// 删除或者选择商品之后，需要通知结算按钮，更新自己的数据；
			if (mChangeListener != null && infos != null) {
				mChangeListener.onDataChange(infos[0], infos[1]);
			}
		}

		@Override
		public int getChildType(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			if (AllEdit) {
				return 1;
			} else if (GroupEdit) {
				return 2;
			} else {
				return 0;
			}
		}

		@Override
		public int getChildTypeCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		public OnClickListener getListener() {
			return listener;
		}

		public void setCheckInterface(CheckInterface checkInterface) {
			this.checkInterface = checkInterface;
		}

		public void setModifyCountInterface(
				ModifyCountInterface modifyCountInterface) {
			this.modifyCountInterface = modifyCountInterface;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return goodsList.get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return goodsList.size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return goodsList;
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return 1;
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}

		View.OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.all_img:
					toast("all");
					AllSelect = selectAll(goodsList, AllSelect, (ImageView) v);
					notifyDataSetChanged();
					break;
				case R.id.edit_all:
					GroupEdit = false;
					AllEdit = true;
					AllSelect = true;

					notifyDataSetChanged();
					break;
				case R.id.group_edit:
					AllEdit = false;
					GroupEdit = true;
					AllSelect = true;
					// goodsList.get((int) v.getTag()).setEditing(true);
					toast("group");
					notifyDataSetChanged();
					break;
				case R.id.check_box_img:
					Select = Singleselect(goodsList, Select, (ImageView) v,
							(int) v.getTag());
					goodsList.get((int) v.getTag()).setSelected(true);
					break;
				case R.id.jian:
					break;
				case R.id.jia:
					break;

				default:
					break;
				}
			}
		};

		public boolean selectAll(List<ShopCartModel> list, boolean isSelectAll,
				ImageView ivCheck) {
			toast("all" + isSelectAll);
			isSelectAll = !isSelectAll;
			checkItem(isSelectAll, ivCheck, 0);
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setSelected(isSelectAll);
			}
			return isSelectAll;
		}

		public boolean Singleselect(List<ShopCartModel> list, boolean isSelect,
				ImageView ivCheck, int position) {
			if (list.get(position).isSelected() == true) {
				toast("true..." + isSelect);
				checkItem(isSelect, ivCheck, 0);

			} else if (list.get(position).isSelected() == false) {
				checkItem(isSelect, ivCheck, 0);
				toast("false..." + isSelect);
			}
			list.get(position).setSelected(isSelect);
			isSelect = !isSelect;
			return isSelect;
		}

		public boolean checkItem(boolean isSelect, ImageView ivCheck, int type) {
			if (type == 0) {
				if (isSelect) {
					ivCheck.setImageResource(R.drawable.check_select);
				} else {
					ivCheck.setImageResource(R.drawable.check_unselect);
				}
			} else {
				if (isSelect) {
					ivCheck.setImageResource(R.drawable.collect_edit);
				} else {
					ivCheck.setImageResource(R.drawable.colloct_save);
				}
			}

			return isSelect;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ChildViewHolder childViewHolder = null;
			if (convertView == null) {
				childViewHolder = new ChildViewHolder();

				convertView = LayoutInflater.from(context).inflate(
						R.layout.shaopcar_childview, null);
				childViewHolder.check_box_img = (ImageView) convertView
						.findViewById(R.id.check_box_img);
				childViewHolder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				childViewHolder.goods_name1_tv = (TextView) convertView
						.findViewById(R.id.goods_name1_tv);
				childViewHolder.goods_detail_tv = (TextView) convertView
						.findViewById(R.id.goods_detail_tv);
				childViewHolder.color_tv = (TextView) convertView
						.findViewById(R.id.color_tv);
				childViewHolder.price_tv = (TextView) convertView
						.findViewById(R.id.price_tv);
				childViewHolder.price_unit_tv = (TextView) convertView
						.findViewById(R.id.price_unit_tv);
				childViewHolder.num_tv = (TextView) convertView
						.findViewById(R.id.num_tv);
				childViewHolder.jian = (TextView) convertView
						.findViewById(R.id.jian);
				childViewHolder.jia = (TextView) convertView
						.findViewById(R.id.jia);
				childViewHolder.type3_jian = (TextView) convertView
						.findViewById(R.id.type3_jian);
				childViewHolder.type3_jia = (TextView) convertView
						.findViewById(R.id.type3_jia);
				childViewHolder.delete = (TextView) convertView
						.findViewById(R.id.delete);
				childViewHolder.type_1 = (LinearLayout) convertView
						.findViewById(R.id.type_1);
				childViewHolder.type_3 = (LinearLayout) convertView
						.findViewById(R.id.type_3);
				childViewHolder.type_2 = (RelativeLayout) convertView
						.findViewById(R.id.type_2);
				convertView.setTag(childViewHolder);
			} else {
				childViewHolder = (ChildViewHolder) convertView.getTag();
			}
			childViewHolder.check_box_img.setTag(childPosition);
			childViewHolder.check_box_img.setOnClickListener(listener);
			if (AllEdit) {
				childViewHolder.check_box_img
						.setImageResource(R.drawable.check_select);
				childViewHolder.type_2.setVisibility(View.GONE);
				childViewHolder.type_1.setVisibility(View.GONE);
				childViewHolder.type_3.setVisibility(View.VISIBLE);
			} else if (GroupEdit) {
				childViewHolder.check_box_img
						.setImageResource(R.drawable.check_select);
				childViewHolder.type_2.setVisibility(View.VISIBLE);
				childViewHolder.type_1.setVisibility(View.GONE);
				childViewHolder.type_3.setVisibility(View.GONE);
			} else if (AllSelect) {
				childViewHolder.check_box_img
						.setImageResource(R.drawable.check_select);
				all_img.setImageResource(R.drawable.check_select);
			} else {
				childViewHolder.check_box_img
						.setImageResource(R.drawable.check_unselect);
				childViewHolder.type_2.setVisibility(View.GONE);
				childViewHolder.type_1.setVisibility(View.VISIBLE);
				childViewHolder.type_3.setVisibility(View.GONE);
			}
			XImageLoader.load(
					GlobalConfig.IMAGE_DOUBLE
							+ goodsList.get(childPosition).getId(),
					childViewHolder.goods_img);
			childViewHolder.goods_name1_tv.setText(goodsList.get(childPosition)
					.getName());
			childViewHolder.goods_detail_tv.setText(goodsList
					.get(childPosition).getItem_status_desc());
			childViewHolder.color_tv.setText(goodsList.get(childPosition)
					.getSku_desc());
			childViewHolder.price_tv.setText(goodsList.get(childPosition)
					.getPrice());
			childViewHolder.price_unit_tv.setText("/"
					+ goodsList.get(childPosition).getUnit_desc());
			childViewHolder.num_tv.setText("X"
					+ goodsList.get(childPosition).getCount());
			childViewHolder.jian.setOnClickListener(listener);
			childViewHolder.jia.setOnClickListener(listener);
			return convertView;
		}

		class ChildViewHolder {
			ImageView check_box_img;
			ImageView goods_img;
			TextView goods_name1_tv, goods_detail_tv, color_tv, price_tv,
					price_unit_tv, num_tv, jian, jia, type3_jian, type3_jia,
					delete;
			LinearLayout type_1, type_3;
			RelativeLayout type_2;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			GroupViewHolder groupViewHolder = null;
			if (convertView == null) {
				groupViewHolder = new GroupViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.shopcart_group, null);
				groupViewHolder.publish_name = (TextView) convertView
						.findViewById(R.id.publish_name);
				groupViewHolder.publish_area = (TextView) convertView
						.findViewById(R.id.publish_area);
				groupViewHolder.edit = (TextView) convertView
						.findViewById(R.id.group_edit);
				convertView.setTag(groupViewHolder);
			} else {
				groupViewHolder = (GroupViewHolder) convertView.getTag();
			}
			// groupViewHolder.publish_name.setText("发布人："
			// + goodsList.get(0).getContact_name());
			groupViewHolder.publish_area.setText(""
					+ TimeFormat.DateFormat(goodsList.get(0).getCreate_time()));
			// groupViewHolder.publish_area.setText((goodsList.get(0)
			// .getPlace_origin()));
			groupViewHolder.edit.setTag(groupPosition);
			groupViewHolder.edit.setOnClickListener(listener);
			return convertView;
		}
	}

	class GroupViewHolder {
		TextView publish_name, publish_area, edit;
	}

	@Override
	public void doIncrease(int groupPosition, int childPosition,
			View showCountView, boolean isChecked, String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doDecrease(int groupPosition, int childPosition,
			View showCountView, boolean isChecked, String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void checkGroup(int groupPosition, boolean isChecked) {
		// TODO Auto-generated method stub

	}

	@Override
	public void checkChild(int groupPosition, int childPosition,
			boolean isChecked) {
		// TODO Auto-generated method stub

	}
}
